package com.gome.oa;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.gome.oa.handler.WindowPopupHandler;
import com.gome.oa.listener.BrowserStatusListener;
import com.gome.oa.listener.MainNavigationAdapter;
import com.gome.oa.util.ResourceUtil;
import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.BrowserFeatures;
import com.teamdev.jexplorer.BrowserMode;
import com.teamdev.jexplorer.LoggerProvider;

/**
 *
 * @author: tang li
 * @date: 2016年8月6日
 * @desc:
 *
 */
public class OAHelper extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private Browser browser = new Browser();
	
	private JTextField txtAddressBar = new JTextField();
	
	private JLabel lblStatusBar = new JLabel();
	
	static {
		BrowserFeatures.enableBrowserMode(BrowserMode.IE8_IGNORE_DOCTYPE);
	}
	
	public OAHelper(String url) {
		setIconImage(ResourceUtil.getImageResource("/com/gome/oa/logo.png"));
		lblStatusBar.setPreferredSize(new Dimension(200, 20));
		add(txtAddressBar, BorderLayout.NORTH);
		add(browser, BorderLayout.CENTER);
		add(lblStatusBar, BorderLayout.SOUTH);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setSize(800, 600);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setLocationRelativeTo(null);
		
		browser.navigate(url);
		browser.addNavigationListener(new MainNavigationAdapter(txtAddressBar));
		browser.addStatusListener(new BrowserStatusListener(this, lblStatusBar));
		browser.setPopupHandler(new WindowPopupHandler(this));
		
		txtAddressBar.setEnabled(false);
	}
	
	public static void main(String[] args) throws Exception {
		LoggerProvider.getBrowserLogger().setLevel(Level.SEVERE);
		LoggerProvider.getIPCLogger().setLevel(Level.SEVERE);
		LoggerProvider.getProcessLogger().setLevel(Level.SEVERE);
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				OAHelper oa = new OAHelper("http://portal.emp.gome.inc/");
				oa.setVisible(true);
			}
		});
	}

}
